package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.oper.OperService;
import tt.dz.biz.service.user.RoleService;
import tt.dz.biz.service.user.SystemInfoService;
import tt.dz.common.DateUtil;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.DzErrorCode.UserErrorCode;
import tt.dz.common.RPCUtil;
import tt.dz.vo.Operator;
import tt.dz.vo.RoleVO;
import tt.dz.vo.SystemInfoVO;

@Service("rpcRoleService")
public class RPCRoleService
  implements RPCBaseService
{
  @Resource(name="roleService")
  private RoleService roleService;
  @Resource(name="operService")
  private OperService operService;
  @Resource(name="systemInfoService")
  private SystemInfoService systemInfoService;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
  {
    String name = message.getName();
    Object reqJson = ((GenericRecord)request).get("req");
    String resJson = "";
    if ("queryRoleForSys".equals(name)) {
      resJson = queryRole(reqJson.toString());
    }
    if ("addSysRole".equals(name)) {
      resJson = addSysRole(reqJson.toString());
    }
    return resJson;
  }
  
  public String queryRole(String reqJson)
  {
    JSONObject resObj = null;
    try
    {
      JSONObject reqObj = JSON.parseObject(reqJson);
      String sysCode = reqObj.getString("sysCode");
      if (StringUtils.isBlank(sysCode))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "必传参数有缺失！");
        
        return JSON.toJSONString(resObj);
      }
      RoleVO roleVO = new RoleVO();
      roleVO.setSysCode(sysCode);
      List<RoleVO> roleList = this.roleService.queryRoleBySys(roleVO);
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(1), null, null);
      resObj.put("result", roleList);
    }
    catch (JSONException e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.FORMAT_ERROR.value(), DzErrorCode.CommonErrorCode.FORMAT_ERROR
        .getDesc());
    }
    catch (Exception e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), "角色列表异常,请稍后重试！");
    }
    return JSON.toJSONString(resObj);
  }
  
  public String addSysRole(String reqJson)
  {
    JSONObject resObj = new JSONObject();
    JSONObject reqObj = JSON.parseObject(reqJson);
    String sysCode = reqObj.getString("sysCode");
    String roleId = reqObj.getString("roleId");
    String roleName = reqObj.getString("roleName");
    String createdBy = reqObj.getString("createdBy");
    if (StringUtils.isBlank(sysCode))
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.CommonErrorCode.PARAM_REQUIRED.value());
      resObj.put("errorMsg", "sysCode为必传参数！");
      return JSON.toJSONString(resObj);
    }
    if (StringUtils.isBlank(roleId))
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.CommonErrorCode.PARAM_REQUIRED.value());
      resObj.put("errorMsg", "roleId为必传参数！");
      return JSON.toJSONString(resObj);
    }
    if (StringUtils.isBlank(roleName))
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.CommonErrorCode.PARAM_REQUIRED.value());
      resObj.put("errorMsg", "roleName为必传参数！");
      return JSON.toJSONString(resObj);
    }
    if (StringUtils.isBlank(createdBy))
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.CommonErrorCode.PARAM_REQUIRED.value());
      resObj.put("errorMsg", "createdBy为必传参数！");
      return JSON.toJSONString(resObj);
    }
    Operator opera = this.operService.queryOperatorByID(createdBy);
    if (opera == null)
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.CommonErrorCode.CREATEBY_NOT_FOUNT.value());
      resObj.put("errorMsg", "查询不到createdBy:" + createdBy + "的操作人员信息，请核实！");
      return JSON.toJSONString(resObj);
    }
    SystemInfoVO systemInfoVo = this.systemInfoService.querySystemInfoById(sysCode);
    if (systemInfoVo == null)
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.CommonErrorCode.SYSCODE_NOT_FOUNT.value());
      resObj.put("errorMsg", "查询不到sysCode:" + sysCode + "的系统信息，请核实！");
      return JSON.toJSONString(resObj);
    }
    RoleVO roleVo = this.roleService.queryRoleByID(roleId, sysCode);
    if (roleVo != null)
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.UserErrorCode.ROLE_EXISTS.value());
      resObj.put("errorMsg", "已经存在roleId:" + roleId + "，sysCode：" + sysCode + "的角色对象信息，请核实！");
      return JSON.toJSONString(resObj);
    }
    roleVo = new RoleVO();
    roleVo.setRoleId(roleId);
    roleVo.setSysCode(sysCode);
    roleVo.setRoleName(roleName);
    roleVo.setCreatedBy(createdBy);
    roleVo.setCreatedTime(DateUtil.getDBCurrentTime());
    roleVo.setIsValid(Integer.valueOf(1));
    boolean excuteFlag = this.roleService.insertRole(roleVo);
    if (excuteFlag)
    {
      resObj.put("isSuccess", "1");
    }
    else
    {
      resObj.put("isSuccess", "0");
      resObj.put("errorCode", DzErrorCode.UserErrorCode.SAVE_UNKNOWN_ERROR.value());
      resObj.put("errorMsg", "保存发生未知错误，请与管理员联系！");
      return JSON.toJSONString(resObj);
    }
    return JSON.toJSONString(resObj);
  }
}
